package com.admin4j.server.auth.multi;

import com.admin4j.framework.security.multi.MultiAuthenticationToken;
import com.admin4j.framework.security.multi.MultiUserDetailsService;
import com.admin4j.server.system.convert.SysUserConvert;
import com.admin4j.server.system.entity.SysUser;
import com.admin4j.server.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

/**
 * 验证码手机号登录
 *
 * @author andanyang
 * @since 2023/11/7 15:39
 */
@Component
public class PhoneMultiUserDetailsService implements MultiUserDetailsService {
    @Autowired
    ISysUserService sysUserService;

    @Override
    public String support() {
        return "phone";
    }

    @Override
    public boolean preVerify(MultiAuthenticationToken multiAuthenticationToken) {

        // 验证码
        String verificationCode = multiAuthenticationToken.getAuthParameter("verificationCode");
        // TODO 验证验证码正确性。错误返回 false,或者抛出错误
        return true;
    }

    @Override
    public UserDetails loadUserByMultiToken(String phoneNumber) {

        SysUser sysUser = sysUserService.getByPhoneNumber(phoneNumber);
        return SysUserConvert.INSTANCE.convert(sysUser);
    }
}
